def binary_search(arr, target):
    low, high = 0, len(arr) - 1
    while low <= high:
        # 确定中间值
        mid = (low + high) // 2
        # 如果中间值与目标值相同，找到返回坐标
        if arr[mid] == target:
            return mid
        # 如果中间值小于目标值，移动左边界
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1


arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
target = 5

result = binary_search(arr, target)

if result != -1:
    print(f"目标元素{target}的索引为{result}")
else:
    print(f"目标元素{target}在数组中未找到")
